"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : autocommit
Case Name   : 使用gs_guc set/reload 的方式设置autocommit，合理报错
Create At   : 2023/01/16
@zou_jialiang0501162244
Description :
    1、gs_guc set 方式设置autocommit
    2、gs_guc reload 方式设置autocommit
Expect      :
    1、合理报错
    2、合理报错
History     :
"""

import os
import unittest

from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class TestAutocommit(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.err_msg = 'ERROR: The name of parameter "autocommit" is ' \
                       'incorrect. It is not within the CN/DN support ' \
                       'parameters or it is a read only parameter.'

    def test_autocommit(self):
        text1 = '-----step1:gs_guc set 方式设置autocommit;expect:报错-----'
        self.log.info(text1)
        for value in range(2):
            set_res = self.pri_sh.execute_gsguc(
                'set', self.err_msg, 'autocommit=0')
            self.log.info(set_res)
            self.assertTrue(set_res, f"执行失败: {text1}")

        text2 = '-----step2:gs_guc reload 方式设置autocommit;expect:报错-----'
        self.log.info(text2)
        for value in range(2):
            reload_res = self.pri_sh.execute_gsguc(
                'reload', self.err_msg, 'autocommit=0')
            self.log.info(reload_res)
            self.assertTrue(reload_res, f"执行失败: {text2}")

    def tearDown(self):
        self.log.info("-----无需清理环境-----")
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
